<template>
  <div class="addGoodsAdd">
    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="topTitle"
      :show-close="true"
      :modal-append-to-body="false"
      :close-on-click-modal="false"
      @close="closeDialog"
    >
      <!-- 点击上面的新增   编辑 -->
      <div class="newAddEdit" style="margin: 0 auto; width: 80%">
        <div class="newAddConfig">
          <el-form
            :model="editCartGoodsData"
            ref="editCartGoodsData"
            class="demo-dynamic"
            label-width="191px"
          >
            <div>
              <el-form-item
                label="奖品类型"
                prop="type"
                :rules="[
                  {
                    required: true,
                    message: '请选择奖品类型',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-select
                  disabled
                  v-model="editCartGoodsData.type"
                  clearable
                  placeholder="请选择奖品类型"
                  style="width: 63%"
                >
                  <el-option
                    v-for="item in gooodsLisr"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
            <div v-if="isJxBc">
              <el-form-item
                label="优惠券e-name"
                :rules="[
                  {
                    required: true,
                    message: '请输入优惠券e-name',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input
                  placeholder="请输入优惠券e-name"
                  v-model="editCartGoodsData.ext.coupons"
                  style="width: 63%"
                ></el-input>
              </el-form-item>
            </div>
            <div v-if="isJxJf">
              <el-form-item
                prop="coin"
                label="积分"
                :rules="[
                  {
                    required: true,
                    message: '请输入积分',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input
                  placeholder="请输入积分"
                  v-model="editCartGoodsData.coin"
                  style="width: 63%"
                ></el-input>
              </el-form-item>
            </div>
            <el-form-item
              prop="weightString"
              label="中奖概率"
              :rules="[
                {
                  required: true,
                  message: '请输入中奖概率',
                  trigger: 'blur',
                },
              ]"
            >
              <el-input
                disabled
                placeholder="请输入中奖概率"
                maxlength="15"
                style="width: 63%"
                v-model="editCartGoodsData.weightString"
              ></el-input>
            </el-form-item>
            <div v-if="isJxAll">
              <el-form-item
                prop="prizeName"
                label="奖品名称"
                :rules="[
                  {
                    required: true,
                    message: '请输入奖品名称',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input
                  placeholder="请输入奖品名称"
                  v-clear-emoij
                  maxlength="40"
                  style="width: 63%"
                  v-model="editCartGoodsData.prizeName"
                ></el-input>
              </el-form-item>
              <el-form-item
                prop="limitNum"
                label="单个用户限制中奖的个数"
                :rules="[
                  {
                    required: true,
                    message: '请输入单个用户限制中奖的个数',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input
                  placeholder="请输入单个用户限制中奖的个数"
                  type="number"
                  min="-1"
                  maxlength="40"
                  style="width: 63%"
                  v-model="editCartGoodsData.limitNum"
                ></el-input>
              </el-form-item>

              <el-form-item
                prop="total"
                label="奖品库存"
                :rules="[
                  {
                    required: true,
                    message: '请输入剩余的库存',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input
                  placeholder="请输入剩余的库存"
                  oninput="value=value.replace(/[^0-9.]/g,'')"
                  min="0"
                  maxlength="9"
                  style="width: 63%"
                  v-model="editCartGoodsData.total"
                ></el-input>
              </el-form-item>

              <el-form-item
                prop="ext.winImg"
                label="中奖图(490*490)"
                :rules="[
                  {
                    required: true,
                    message: '请选择中奖图',
                    trigger: 'blur',
                  },
                ]"
              >
                <div class="picCommodity">
                  <el-upload
                    class="upload-demo"
                    :action="uploadUrl + '/api/v2.1/oss/aliyun/upload'"
                    name="files"
                    :data="{ type: 'add_main_' }"
                    :on-success="uploadSuccessConfig"
                    :headers="myHeaders"
                    :on-error="uploadErrorConfig"
                    :before-upload="
                      (file) => {
                        return beforeAvatarUploadConfig(file, 490, 490);
                      }
                    "
                    multiple
                    :file-list="fileListArea"
                    :show-file-list="false"
                  >
                    <img
                      v-show="editCartGoodsData.ext.winImg"
                      :src="editCartGoodsData.ext.winImg"
                      alt
                      style="height: 100px"
                      @click.stop=""
                    />
                    <i
                      v-show="editCartGoodsData.ext.winImg"
                      class="el-icon-error imgDelete"
                      @click.stop="uploadimgDeleteArea()"
                    ></i>
                    <el-button
                      v-show="!editCartGoodsData.ext.winImg"
                      @click="clickUploadAdd"
                      >点击传图</el-button
                    >
                  </el-upload>
                </div>
              </el-form-item>
              <el-form-item
                prop="ext.prizeRecordImg"
                label="中奖列表图(150*150)"
                :rules="[
                  {
                    required: true,
                    message: '请选择中奖列表图',
                    trigger: 'blur',
                  },
                ]"
              >
                <div class="picCommodity">
                  <el-upload
                    class="upload-demo"
                    :action="uploadUrl + '/api/v2.1/oss/aliyun/upload'"
                    name="files"
                    :data="{ type: 'add_main_' }"
                    :on-success="uploadSuccessConfig1"
                    :headers="myHeaders"
                    :before-upload="
                      (file) => {
                        return beforeAvatarUploadConfig(file, 150, 150);
                      }
                    "
                    :on-error="uploadErrorConfig1"
                    multiple
                    :file-list="fileListArea"
                    :show-file-list="false"
                  >
                    <img
                      v-show="editCartGoodsData.ext.prizeRecordImg"
                      :src="editCartGoodsData.ext.prizeRecordImg"
                      alt
                      style="height: 100px"
                      @click.stop=""
                    />
                    <i
                      v-show="editCartGoodsData.ext.prizeRecordImg"
                      class="el-icon-error imgDelete"
                      @click.stop="uploadimgDeleteArea1()"
                    ></i>
                    <el-button
                      v-show="!editCartGoodsData.ext.prizeRecordImg"
                      @click="clickUploadAdd"
                      >点击传图</el-button
                    >
                  </el-upload>
                </div>
              </el-form-item>
              <el-form-item
                prop="ext.indexRotationImg"
                label="首页奖品轮播图(162*140)"
                :rules="[
                  {
                    required: true,
                    message: '请选择首页奖品轮播图',
                    trigger: 'blur',
                  },
                ]"
              >
                <div class="picCommodity">
                  <el-upload
                    class="upload-demo"
                    :action="uploadUrl + '/api/v2.1/oss/aliyun/upload'"
                    name="files"
                    :data="{ type: 'add_main_' }"
                    :on-success="uploadSuccessConfig2"
                    :headers="myHeaders"
                    :before-upload="
                      (file) => {
                        return beforeAvatarUploadConfig(file, 162, 140);
                      }
                    "
                    :on-error="uploadErrorConfig2"
                    multiple
                    :file-list="fileListArea"
                    :show-file-list="false"
                  >
                    <img
                      v-show="editCartGoodsData.ext.indexRotationImg"
                      :src="editCartGoodsData.ext.indexRotationImg"
                      alt
                      style="height: 100px"
                      @click.stop=""
                    />
                    <i
                      v-show="editCartGoodsData.ext.indexRotationImg"
                      class="el-icon-error imgDelete"
                      @click.stop="uploadimgDeleteArea2()"
                    ></i>
                    <el-button
                      v-show="!editCartGoodsData.ext.indexRotationImg"
                      @click="clickUploadAdd"
                      >点击传图</el-button
                    >
                  </el-upload>
                </div>
              </el-form-item>
            </div>
            <el-form-item style="margin-top: 40px">
              <el-button type="danger" @click="closeDialog()">取消</el-button>
              <el-button
                type="primary"
                @click="confirmAddConfig('editCartGoodsData')"
                >确认</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import requestAxios from "../../../http";
export default {
  name: "luckPrizeEdit",
  data() {
    return {
      isJxBc: "",
      isJxJf: "",
      isJxAll: "",
      gooodsLisr: [
        {
          value: 1,
          label: "实体奖",
        },
        {
          value: 3,
          label: "优惠券",
        },
        {
          value: 4,
          label: "积分奖",
        },
        {
          value: -1,
          label: "谢谢惠顾",
        },
      ],
      dialogFormVisible: true, //背景遮罩层
      topTitle: "奖池奖品编辑", //弹出框上面的标题
      uploadUrl: "",
      fileListArea: [], //图片文件上传列表，定制区域部分
      prizeDate: [],
      idss: "",
      editCartGoodsData: {
        limitNum: "",
        coin: "",
        type: "",
        prizeName: "",
        total: "",
        onlineTime: "",
        offlineTime: "",
        ext: {
          winImg: "",
          coupons: "",
          prizeRecordImg: "",
          indexRotationImg: "",
        },
      },
    };
  },
  props: ["addOrEdit", "editGoodsCont", "ids"],
  // 获取token
  created() {
    this.myHeaders = {
      token: sessionStorage.getItem("authorization"),
    };
  },
  mounted() {
    this.uploadUrl = window.uploadUrl;
    this.idss = this.$props.ids;
    this.editCartGoodsData = this.$props.editGoodsCont;
    console.log(this.editCartGoodsData, "获取父组件的数据");
    this.editCartGoodsData.ext = JSON.parse(this.editCartGoodsData.ext);
    console.log(this.editCartGoodsData.ext, "获取ext里面的数据");

    if (this.editCartGoodsData.type == 3) {
      console.log(this.editCartGoodsData.type, "22222yuuio");
      this.isJxBc = true;
    } else {
      this.isJxBc = false;
    }
    if (this.editCartGoodsData.type == 4) {
      this.isJxJf = true;
    } else {
      this.isJxJf = false;
    }
    if (this.editCartGoodsData.type == -1) {
      this.isJxAll = false;
    } else {
      this.isJxAll = true;
    }
  },
  methods: {
    // 点击传图
    clickUploadAdd() {
      console.log("点击传图");
    },
    // 关闭弹框
    closeDialog() {
      console.log("关闭弹窗");
      this.$parent.addOrEdit = 0;
    },
    //图片上传失败
    uploadErrorConfig(err, file, fileListArea) {
      console.log("uploadError", err, file, fileListArea);
      this.tip(false, "图片上传失败");
    },
    uploadErrorConfig1(err, file, fileListArea) {
      console.log("uploadError", err, file, fileListArea);
      this.tip(false, "图片上传失败");
    },
    uploadErrorConfig2(err, file, fileListArea) {
      console.log("uploadError", err, file, fileListArea);
      this.tip(false, "图片上传失败");
    },
    // 图片上传成功
    uploadSuccessConfig(response, file, fileListArea) {
      console.log(response, file, fileListArea);
      if (response.success == false) {
        this.tip(false, "请重新登录");
      } else {
        this.editCartGoodsData.ext.winImg = response.data;
        this.tip(true, "图片上传成功");
      }
    },
    uploadSuccessConfig1(response, file, fileListArea) {
      console.log(response, file, fileListArea);
      if (response.success == false) {
        this.tip(false, "请重新登录");
      } else {
        this.editCartGoodsData.ext.prizeRecordImg = response.data;
        this.tip(true, "图片上传成功");
      }
    },
    uploadSuccessConfig2(response, file, fileListArea) {
      console.log(response, file, fileListArea);
      if (response.success == false) {
        this.tip(false, "请重新登录");
      } else {
        this.editCartGoodsData.ext.indexRotationImg = response.data;
        this.tip(true, "图片上传成功");
      }
    },
    //上传图片部分的操作
    beforeAvatarUploadConfig(file, width, height) {
      console.log("上传图片", file);
      // return file;
      return this.beforeAvatarUpload(file, 5, width, height);
    },
    //验证图片
    beforeAvatarUpload(file, size = 5, width = 750, height = 1334) {
      console.log("上传图片的宽高限制", width, height);
      let that = this;
      console.log("验证图片", file.type);
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < size;
      if (!isJPG && !isPNG) {
        this.$message.error("上传图片只能是 PNG/JPG 格式!");
        return false;
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 5MB!");
        return false;
      }

      //判断图片尺寸
      const isSize = new Promise((resolve, reject) => {
        let _URL = window.URL || window.webkitURL;
        let img = new Image();
        img.onload = function () {
          if (width && height) {
            let valid = img.width == width && img.height == height;
            valid ? resolve() : reject();
          } else {
            if (width) {
              let valid = img.width == width;
              valid ? resolve() : reject();
            } else {
              let valid = img.height == height;
              valid ? resolve() : reject();
            }
          }
        };
        img.src = _URL.createObjectURL(file);
      }).then(
        () => {
          that.imgUploadLetterLoding = true;
          return file;
        },
        () => {
          if (width && height) {
            that.$message.error(`上传图片的尺寸必须为 ${width}*${height}`);
          } else {
            if (width) {
              that.$message.error(`上传图片的宽度必须为 ${width}`);
            } else {
              that.$message.error(`上传图片的高度必须为 ${height}`);
            }
          }
          return Promise.reject();
        }
      );
      return isSize;
    },
    // 图片删除
    uploadimgDeleteArea() {
      this.editCartGoodsData.ext.winImg = "";
    },
    uploadimgDeleteArea1() {
      this.editCartGoodsData.ext.prizeRecordImg = "";
    },
    uploadimgDeleteArea2() {
      this.editCartGoodsData.ext.indexRotationImg = "";
    },

    //新增确定调取接口
    confirmAddConfig(formName) {
      console.log(formName, "wwww");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.editCartGoodsData.ext) {
            this.editCartGoodsData.ext = JSON.stringify(
              this.editCartGoodsData.ext
            );
          }
          let obj = this.editCartGoodsData;
          requestAxios
            .fetchPost(
              "/api/v2.1/exchange/store/management/prize/prizeGroupDetail/update?id=" +
                this.idss,
              obj,
              {
                headers: {},
              }
            )
            .then((data) => {
              if (data.status == 200 && data.data.success) {
                console.log("新增奖品结果", data.data);
                //新增成功
                this.$emit("getAddGoodsList");
                this.tip(true, "编辑奖池奖品成功!");
                this.$parent.addOrEdit = 0;
              } else {
                this.tip(false, data.data.errorMsg);
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          // 获取图形验证码
          this.$message.error("请完善商品信息！");
          return false;
        }
      });
    },

    //提示的通用方法
    tip(state, text) {
      if (state) {
        this.$message({
          showClose: true,
          message: text,
          type: "success",
        });
      } else {
        this.$message({
          showClose: true,
          message: text,
          type: "error",
        });
      }
    },
  },
};
</script>
<style>
.addGoodsAdd .close_boun {
  position: absolute;
  font-size: 26px;
  color: white;
  top: 15px;
  right: 10px;
}
.configBounDele {
  margin-right: 46px;
}
.el-icon-error {
  margin-left: 50px;
  font-size: 17px;
  color: rgb(133, 131, 131);
}
.enterInput {
  width: 51%;
  outline: none;
  padding-left: 7px;
}
.disApperInput {
  margin-left: 13%;
}
.disApperInput input {
  outline: none;
}
.closeSelect {
  width: 27%;
  height: 22px;
  padding-left: 7px;
  display: inline-block;
  margin-right: 10%;
  position: relative;
  margin-bottom: 10px;
}
.selectListX {
  /* width: 70%; */
  margin-left: 12%;
}
.selectListX .el-icon-close {
  position: absolute;
  top: 5px;
  left: 130px;
}
.selectListX .closeSelect:nth-child(2n) {
  margin-right: 0;
}
.affirmAddCancel .el-button--danger:focus,
.affirmAddCancel .el-button--danger {
  width: 93px;
  background: white;
  color: #d4af82;
  border: 1px solid #d4af82;
  margin-right: 20px;
}
.clickUpload {
  display: block;
  width: 93px;
  color: #d4af82;
  font-size: 12px;
  height: 28px;
  line-height: 28px;
  border-radius: 4px;
  border: 1px solid #d4af82;
  text-align: center;
  margin-left: 22%;
}
.affirmAddCancel {
  margin-top: 40px;
  text-align: center;
}
.affirmAddCancel .el-button--primary {
  width: 93px;
  /* background: #d4af82; */
  border: none;
}
.el-dialog__header {
  background: #77b3a1;
  text-align: center;
}
.el-dialog__title {
  color: white;
}
.el-dialog__headerbtn .el-dialog__close {
  color: white;
  font-size: 26px;
}

/*
传图页面的样式
*/
.uploadPicList {
  height: 100px;
  display: flex;
}
.picCommodity {
  height: 100px;
  display: inline-block;
  width: 380px;
  position: relative;
}
.uploadPicName {
  display: inline-block;
  margin-left: 100px;
  width: 150px;
  line-height: 100px;
}
.picCommodity .imgDelete {
  /* color: #d4af82; */
  position: absolute;
  left: -68px;
  top: -7px;
}
.picCommodity img {
  position: absolute;
  left: 0px;
  top: 0px;
}
/* 确定和取消按钮 */
.configUploadPage .affirmAddCancel .el-button--danger:focus,
.configUploadPage .affirmAddCancel .el-button--danger {
  width: 93px;
  background: white;
  /* color: #d4af82;
  border: 1px solid #d4af82; */
  margin-right: 20px;
}
.configUploadPage .affirmAddCancel .el-button--primary {
  width: 93px;
  /* background: #d4af82; */
  border: none;
}
.configUploadPage {
  width: 80%;
  margin: 0 auto;
}
</style>
